﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zyg.Utility.Helper;

namespace Zyg.SSO.Core.Business
{
    public class AccountAppUser : BusinessBase<AccountAppUser, Guid>
    {
        public AccountAppUser()
        { }

        #region Fields and Properties

        private string name = String.Empty;
        private string password = String.Empty;
        private Guid accountAppID = Guid.Empty;

        /// <summary>
        /// Get the user name in the app.
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    MarkChanged("Name");
                    name = value;
                }
            }
        }

        /// <summary>
        /// Get or set the password in the app.
        /// </summary>
        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    MarkChanged("Password");
                    password = value;
                }
            }
        }

        /// <summary>
        /// Get or set the accountappid.
        /// </summary>
        public Guid AccountAppID
        {
            get { return accountAppID; }
            set
            {
                if (accountAppID != value)
                {
                    MarkChanged("AccountAppID");
                    accountAppID = value;
                }
            }
        }

        private AccountApp accountApp;
        /// <summary>
        /// Get the AccountApp relation that the user belongs to.
        /// </summary>
        public AccountApp AccountApp
        {
            get
            {
                if (accountApp == null || accountApp.Id != AccountAppID)
                    LoadAccountApp();
                return accountApp;
            }
        }

        #endregion

        #region Base Overrides

        protected override void ValidationRules()
        {
            AddRule("Id", "标识不合法", Id == System.Guid.Empty);
            AddRule("Name", "用户名长度应该小于100", !Name.IsInLengthBound(0, 100));
            AddRule("Password", "用户密码长度应该小于50", !Password.IsInLengthBound(0, 50));
        }

        protected override AccountAppUser DataSelect(Guid id)
        {
            return Services.DALService.SelectAccoutAppUser(id);
        }

        protected override void DataUpdate()
        {
            Services.DALService.UpdateAccoutAppUser(this);
        }

        protected override void DataInsert()
        {
            Services.DALService.InsertAccoutAppUser(this);
        }

        protected override void DataDelete()
        {
            Services.DALService.DeleteAccoutAppUser(this);
        }

        #endregion

        #region Protected Methods

        private void LoadAccountApp()
        {
            accountApp = AccountApp.Load(AccountAppID);
        }

        #endregion
    }
}
